package codegenerator.generator;

import baytony.util.Util;
import codegenerator.CodeGeneratorException;
import codegenerator.CommonHelper;
import codegenerator.FileBean;
import codegenerator.TableInfo;
import codegenerator.XmlFileBean;

public class TilesGenerator extends XmlGenerator {

	private static final String DEFINITION = "<definition name=\"#className#.index\" extends=\"main.page\">" +
											 "	<put-attribute name=\"body\" value=\"/WEB-INF/jsp/#className#/index.jsp\"/>" +
											 "</definition>";
	private static final String CLASS_NAME = "#className#";
	private static final String TILES = "tiles.jtmp";
	private static final String DEFINITION_CONTENT = "#definition#";

	@Override
	public FileBean createInfo(TableInfo... tables) throws CodeGeneratorException {
		if(Util.isEmptyArray(tables)) {
			throw new CodeGeneratorException("*** No tables error ***");
		}
		StringBuffer sb = new StringBuffer();
		for(TableInfo table : tables) {
			String temp = DEFINITION;
			String tableName = table.getName();
			temp = temp.replaceAll(CLASS_NAME, CommonHelper.table2ClassName(tableName));
			sb.append(temp);
		}
		String jtmpContent = super.readJtmp(TILES);
		jtmpContent = jtmpContent.replaceAll(DEFINITION_CONTENT, sb.toString());
		FileBean result = new XmlFileBean();
		result.setFileName("tiles");		
		result.setContent(jtmpContent);
		result.setPath(generatorHelper.getConfigOutputPath());
		return result;
	}

}
